<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/wesite/view/common/base.jsp"%>
<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>登录</title>
    <%@ include file="/wesite/view/common/common.jsp"%>
    <style type="text/css">
        html,body{background:#f2f2f2;}
    </style>
</head>
<body>
    <div class="loginbox">
        <form id="loginForm" action="${ctx}/we/user/loginUser.json" method="post">
            <div class="login-input">
                <div class="input-text">

                    <span class="text-tit">用户名</span>
                    <input id="username" name="username" type="text" placeholder="请输入手机号码或用户名">
                </div>
                <div class="input-text"><span class="text-tit">密码</span><input id="password" name="password" type="password" placeholder="请输入登录密码"></div>
                <div class="input-text">
                    <span class="text-tit">验证码</span>
                    <input id="code" name="code" type="number" placeholder="请输入验证码" maxlength="6" >
                    <img style="float: right; position: relative; top: 0.17rem; right: .533333rem; height: 1.8rem; " tsrc="${ctx}/validateimg" src="${ctx}/validateimg?<%=Math.random()%>" onclick="javascript:$(this).attr('src', $(this).attr('tsrc')+'?'+new Date().getTime());" id="checkcodeimg" />
                </div>

            </div>
            <div class="input-text binding-wechat" style="border: none;display: block;">
                <label style="width: 100%;display: block; text-align: right;">
                    <span style="float: left; vertical-align: middle;">绑定当前微信</span>
                    <input id="bindwechat" name="bindwechat" style=" vertical-align: middle;" type="checkbox" class="aui-switch" checked>
                </label>
            </div>
            <input id="btnLogin" type="button" value="登录" onclick="javascript:$('#loginForm').submit();" />
        </form>
        <div class="login-b">
            <a class="fl" href="/we/sign/register">注册</a>
            <a class="fr" href="/we/user/resetPassword">忘记密码？</a>
        </div>
    </div>

<script src="${webctx}/script/aui-toast.js"></script>
<script>

    apiready = function(){
        api.parseTapmode();
    }

    var toast = new auiToast({});

    $(function(){

        $("#loginForm").submit(function(){
            var username = $("#username").val().trim();
            var password = $("#password").val().trim();
            var code = $("#code").val().trim();
            var bindwechat = $("#bindwechat").is(':checked');
            if(username==""){
                showToastMsg("fail","用户名为空");
                return false;
            }
            if(password==""){
                showToastMsg("fail","密码为空");
                return false;
            }
            if(code==""){
                showToastMsg("fail","验证码为空");
                return false;
            }

            $("#btnLogin").attr("disabled",true);
            showToastLoading("正在登录...");

            $w.httpRequest({
                url:apiroot+"/we/user/loginUser",
                data:{"loginname":username,"pwd":password,"checkcode":code,'bindwechat':bindwechat},
                ok:function(ret){

                    var user=ret.Data;
                    $w.setLoginUser(user.userid,user.title,user.phone);
                    var tourl="";
                    if(sessionStorage.getItem("_curUrl_"))tourl=sessionStorage.getItem("_curUrl_");
                    else if($("[name='requesturl']").length>0)tourl=$("[name='requesturl']").val()
                    else tourl=ctx;
                    if(tourl.indexOf("user/showlogin")!=-1)tourl=ctx+"/website/index.jsp";
                    sessionStorage.removeItem("_curUrl_");
                    var bl=true;
                    try{
                        if(sessionStorage.getItem("_newreg_")=='true'){
                            bl=false;
                            //window.location.href=ctx+"/user/certification";
                              window.location.href=ctx+"/we/index";
                        }
                        sessionStorage.removeItem("_newreg_");
                    }catch(e){}

                    if(bl){
                        hideToastLoading();
                        showToastMsg("success","登录成功");
                        setTimeout(function(){
                            if(bl)window.location.href= ctx+"/we/index";
                        },1000);
                    } else {
                        showToastMsg("fail","登录失败");
                    }

                },fail:function(ret){

                    hideToastLoading();

                    if(ret&&ret.MsgDesc){
                        showToastMsg("fail",ret.MsgDesc);
                    }else{
                        showToastMsg("fail","登录失败");
                    }

                    $("#btnLogin").attr("disabled",false);

                    return false;
                }
            });

            return false;

        });
    });

    function showToastMsg(type,msg){

        switch (type) {
            case "success":
                toast.success({
                    title: msg,
                    duration: 2000
                });
                break;
            case "fail":
                toast.fail({
                    title: msg,
                    duration: 2000
                });
                break;
            default:
                break;
        }
    }

    function showToastLoading(msg){
        toast.loading({
            title: msg
        });
    }

    function hideToastLoading(){
        toast.hide();
    }

</script>

</body>

</html>